package com.hspedu.object_;

import com.hspedu.poly.polyparameter_.Manager;

public class ToString_ {
    public static void main(String[] args) {
        /*
        Object 的 toString() 源码
        (1)getClass().getName() 类的全类名(包名+类名)
        (2)Integer.toHexString(hashCode()) 将对象的hashCode的值转换为16进制字符串
        public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }
         */

        Monster monster = new Monster("小妖怪", "巡山", 1000);
        System.out.println(monster.toString() + " hashCode=" + monster.hashCode());

        //当直接输出一个对象时，toString 方法会被默认调用
        System.out.println(monster);
    }
}


class Monster {

    private String name;
    private String job;
    private double salary;

    public Monster(String name, String job, double salary) {
        this.name = name;
        this.job = job;
        this.salary = salary;
    }

    //重写 toString 方法，输出对象属性
    @Override
    public String toString() {
        return "Monster{" +
                "name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", salary=" + salary +
                '}';
    }
}
